<?php
/**
 * ============================================================================
 * Copyright (c) 2015-2018 贵州大师兄信息技术有限公司 All rights reserved.
 * siteַ: http://www.dsxcms.com
 * ============================================================================
 * @author:     David Song<songdewei@163.com>
 * @version:    v1.0.0
 * ---------------------------------------------
 * Date: 2018/11/26
 * Time: 11:21 AM
 */

namespace App\Library\Mall\Shop;


use Illuminate\Http\Request;

trait CatlogManagers
{
    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(Request $request)
    {
        $newcatlog = array_only($request->input('catlog'), [
            'name',
            'fid',
            'displayorder'
        ]);

        $catid = $request->input('catid', 0);

        if ($catid)
        {
            $catlog = $this->shop->catlogs()->find($catid);
            if ($catlog)
            {
                $catlog->update($newcatlog);
            }
        } else {
            $catlog = $this->shop->catlogs()->create($newcatlog);
        }

        return $this->sendCatlogSavedResponse($request, $catlog);
    }

    /**
     * @param Request $request
     * @param $catlog
     * @return \Illuminate\Http\JsonResponse
     */
    protected function sendCatlogSavedResponse(Request $request, $catlog)
    {
        return ajaxReturn(['catlog'=>$catlog]);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getCatlog(Request $request)
    {
        $catid = $request->input('catid', 0);
        $catlog = $this->shop->catlogs()->find($catid);
        return ajaxReturn(['catlog'=>$catlog]);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getCatlogList(Request $request)
    {
        $items = $this->shop->catlogs()->with('children')->where('fid', 0)->orderBy('displayorder')->orderBy('catid')->get();
        return ajaxReturn(['items'=>$items]);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete(Request $request)
    {
        $catid = $request->input('catid');
        $catlog = $this->shop->catlogs()->find($catid);
        if ($catlog)
        {
            $catlog->children()->update(['fid'=>$catlog->fid]);
            $catlog->delete();
        }
        return $this->sendDeletedResponse($request);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    protected function sendDeletedResponse(Request $request)
    {
        return ajaxReturn();
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function changeToTop(Request $request)
    {
        $catid = $request->input('catid');
        $this->shop->catlogs()->where('catid', $catid)->update(['fid'=>0]);
        return ajaxReturn();
    }
}
